package com.ll.beautymall.utils;

import com.ll.beautymall.api.UrlConfig;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2016/10/20.
 */
public class HttpUtils {
    private static volatile HttpUtils httpUtils;
    private IHomeApi homeApi;
    private final HashMap<String, String> map;
    private final OkHttpClient client;
    private final IHomeApi iVapi;

    private HttpUtils() {
        client = new OkHttpClient.Builder()
                .readTimeout(5, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
//                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(UrlConfig.Url.BASE_URL)
                .client(client)
                .build();

        Retrofit retrofitVapi = new Retrofit.Builder()
                .addConverterFactory(ScalarsConverterFactory.create())
                .baseUrl(UrlConfig.Url.BASE_VAPI_URL)
                .client(client)
                .build();

        map = new HashMap<>();
        map.put(UrlConfig.Key.YK_PID, UrlConfig.Value.VALIUE_YK_PID);
        map.put(UrlConfig.Key.YK_APPID, UrlConfig.Value.VALIUE_YK_APPID);
        map.put(UrlConfig.Key.YK_CC, UrlConfig.Value.VALIUE_YK_CC);
        map.put(UrlConfig.Key.YK_CVC, UrlConfig.Value.VALIUE_YK_CVC);

        homeApi = retrofit.create(IHomeApi.class);
        iVapi = retrofitVapi.create(IHomeApi.class);
    }

    public static HttpUtils getInstance() {
        if (httpUtils == null) {
            synchronized (HttpUtils.class) {
                if (httpUtils == null) {
                    httpUtils = new HttpUtils();
                }
            }
        }
        return httpUtils;
    }

    public void getNavData(Callback<String> callback) {
        homeApi.getNav()
                .enqueue(callback);
    }

    public void getNineNavData(Callback<String> callback) {
        homeApi.getNineNavData()
                .enqueue(callback);
    }

    //Banner是不变的
    public void getBannerData(HashMap<String, String> parameters, Callback<String> callback) {
        map.put(UrlConfig.Key.MID, parameters.get(UrlConfig.Key.MID));
        map.put(UrlConfig.Key.CURSOR, UrlConfig.Value.VALIUE_CURSOR);
        map.put(UrlConfig.Key.SORT, UrlConfig.Value.VALIUE_SORT_DEFAULT);
        map.put(UrlConfig.Key.COUNT, UrlConfig.Value.VALIUE_COUNT);
        homeApi.getBanner(map)
                .enqueue(callback);
    }

    public void getModelData(HashMap<String, String> parameters, Callback<String> callback) {
        // "?mid=457465&yk_pid=3&yk_appid=1&yk_cc=m360&yk_cvc=311&cursor=0&count=40"
        map.put(UrlConfig.Key.MID, parameters.get(UrlConfig.Key.MID));
        map.put(UrlConfig.Key.SORT, UrlConfig.Value.VALIUE_SORT_DEFAULT);
        homeApi.getModel(map)
                .enqueue(callback);
    }

    public void getAllBuyData(HashMap<String, String> parameters, Callback<String> callback) {
        HashMap<String, String> map = new HashMap<>();
        //第一级的
        //"mid", "type","category_ids""channel_id""sort",

        //第二级：分类
        //mid=457465&type=dress&category_ids=22170&cursor=0&count=40&sort=default
        // &yk_pid=3&yk_appid=1&yk_cc=m360&yk_cvc=311
        if (parameters.get(UrlConfig.Key.ISONE) == "1") {
            map.put(UrlConfig.Key.CHANNEL_ID, parameters.get(UrlConfig.Key.CHANNEL_ID));//"1464783721"
        }
        map.put(UrlConfig.Key.MID, parameters.get(UrlConfig.Key.MID));
        map.put(UrlConfig.Key.SORT, parameters.get(UrlConfig.Key.SORT));
        map.put(UrlConfig.Key.CATEGORY_IDS, parameters.get(UrlConfig.Key.CATEGORY_IDS));//值为："4796,4797,4798,4805,4838,4839,4840,4841,4843,4892,22150,22153,22155");
        map.put(UrlConfig.Key.CURSOR, parameters.get(UrlConfig.Key.CURSOR));
        map.put(UrlConfig.Key.COUNT, parameters.get(UrlConfig.Key.COUNT));
        String part = parameters.get("part");
        if (part != null && part.equals("nine")) {
            homeApi.getNineAllBuyData(map)
                    .enqueue(callback);
        } else {
            String type = parameters.get(UrlConfig.Key.TYPE);
            if (type == null) {
                homeApi.getNineAllBuyData(map)
                        .enqueue(callback);
            } else {
                map.put(UrlConfig.Key.TYPE, parameters.get(UrlConfig.Key.TYPE));
                homeApi.getAllBuyData(map)
                        .enqueue(callback);
            }
        }
    }

    public void getCategoryList(HashMap<String, String> parameters, Callback<String> callback) {
        if (parameters == null) {
            homeApi.getNineNavData()
                    .enqueue(callback);
        } else {
            map.put(UrlConfig.Key.TYPE, parameters.get(UrlConfig.Key.TYPE));
            map.put(UrlConfig.Key.MID, parameters.get(UrlConfig.Key.MID));
            map.put(UrlConfig.Key.SORT, UrlConfig.Value.VALIUE_SORT_DEFAULT);
            map.put(UrlConfig.Key.CURSOR, parameters.get(UrlConfig.Key.CURSOR));
            map.put(UrlConfig.Key.COUNT, parameters.get(UrlConfig.Key.COUNT));
            homeApi.getCategoryList(map)
                    .enqueue(callback);
        }
    }


    public void getBrandList(HashMap<String, String> parameters, Callback<String> callback) {
        map.put(UrlConfig.Key.TYPE, parameters.get(UrlConfig.Key.TYPE));
        map.put(UrlConfig.Key.MID, parameters.get(UrlConfig.Key.MID));
        homeApi.getBrandData(map)
                .enqueue(callback);
    }

    public void getZeroData(HashMap<String, String> parameters, Callback<String> callback) {
        map.put(UrlConfig.Key.TYPE, parameters.get(UrlConfig.Key.TYPE));
        map.put(UrlConfig.Key.OBJECT_ID, parameters.get(UrlConfig.Key.OBJECT_ID));
        map.put(UrlConfig.Key.CURSOR, parameters.get(UrlConfig.Key.CURSOR));
        map.put(UrlConfig.Key.COUNT, parameters.get(UrlConfig.Key.COUNT));
        map.put(UrlConfig.Key.SID, UrlConfig.Value.VALIUE_SID);
        map.put(UrlConfig.Key.MID, UrlConfig.Value.VALIUE_MID);
        iVapi.getZeroData(map)
                .enqueue(callback);
    }

    public void getZeroHeader(HashMap<String, String> parameters, Callback<String> callback) {
        map.put(UrlConfig.Key.ACTIVITY_ID, parameters.get(UrlConfig.Key.ACTIVITY_ID));
        map.put(UrlConfig.Key.SID, UrlConfig.Value.VALIUE_SID);
        map.put(UrlConfig.Key.MID, UrlConfig.Value.VALIUE_MID);
        iVapi.getZeroHeaderData(map)
                .enqueue(callback);
    }

    public void getQualityData(HashMap<String, String> parameters, Callback<String> callback) {
        map.put(UrlConfig.Key.TYPE, UrlConfig.Value.VALIUE_QUALITY_TYPE);
        map.put(UrlConfig.Key.SID, UrlConfig.Value.VALIUE_SID);
        map.put(UrlConfig.Key.MID, UrlConfig.Value.VALIUE_MID);
        map.put(UrlConfig.Key.CURSOR, parameters.get(UrlConfig.Key.CURSOR));
        map.put(UrlConfig.Key.COUNT, parameters.get(UrlConfig.Key.COUNT));
        iVapi.getQualityData(map)
                .enqueue(callback);
    }
}
